package cn.edu.ecut.se2024.controller;

import java.time.LocalDate;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import cn.edu.ecut.se2024.model.Inventory;
import cn.edu.ecut.se2024.service.InventoryService;

@RestController
public class InventoryController {
    
    @Autowired
    private InventoryService inventoryService;

    @PostMapping("/queryInventory")
    public List<Inventory> queryInventory(@RequestBody Map<String, String> searchParams) {
        // Prepare search criteria based on request body parameters
        String itemNumber = searchParams.get("itemNumber");
        double minPrice = Double.parseDouble(searchParams.get("minPrice"));
        double maxPrice = Double.parseDouble(searchParams.get("maxPrice"));
        int quantity = Integer.parseInt(searchParams.get("quantity"));
        LocalDate manufacturingDate = LocalDate.parse(searchParams.get("manufacturingDate"));
        int warehouseNumber = Integer.parseInt(searchParams.get("warehouseNumber"));
        boolean fragile = Boolean.parseBoolean(searchParams.get("fragile"));
        String searchType = searchParams.get("searchType");
        
        // Query inventory based on search criteria
        List<Inventory> inventoryList = inventoryService.queryInventory(itemNumber, minPrice, maxPrice, quantity, manufacturingDate, warehouseNumber, fragile, searchType);
        
        return inventoryList;
    }
}